﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;

namespace JZ.Common
{
    public static class JsonSerializer
    {
        public static T Deserializer<T>(string data)
        {
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(data)))
            {
                return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
            }
        }

        public static string Serializer(object Obj)
        {
            if (Obj == null)
            {
                return string.Empty;
            }

            DataContractJsonSerializer ds = new DataContractJsonSerializer(Obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                ds.WriteObject(ms, Obj);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
    }
}
